package com.two.lbms.ww.controller;

import com.two.lbms.base.BaseController;
import com.two.lbms.base.ResultInfo;
import com.two.lbms.vo.Reader;
import com.two.lbms.ww.query.ReaderInfo;
import com.two.lbms.ww.service.ReaderService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class ReaderController extends BaseController {

    @Resource
    private ReaderService readerService;

    /**
     *  分页多条件查询读者信息
     *
     * @param readerInfo
     * @return
     */
    @RequestMapping("reader")
    @ResponseBody
    public Map<String,Object> queryReaderAllByReader(ReaderInfo readerInfo){
        Map<String,Object> map = readerService.queryReaderAllByReader(readerInfo);
        //System.out.println(map);
        return map;
    }

    /**
     * 进入读者页面
     * @return
     */
    @RequestMapping("toReader")
    public String toReader(){
        return "ww/reader";
    }

    /**
     * 添加读者
     * @param reader
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addReader(Reader reader){
        readerService.addReader(reader);
        return success("读者信息添加成功！");
    }

    /**
     *  更新读者信息
     *
     * @param reader
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateReader(Reader reader){
        readerService.updateReader(reader);
        return success("读者信息更新成功！");
    }

    /**
     *  删除读者信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteReader(Integer[] ids){
        // 调用service层的删除方法
        readerService.deleteReader(ids);
        return success("读者信息删除成功！");
    }

    /**
     * 进入读者信息添加或修改页面
     *
     * @return
     */
    @RequestMapping("add_edit")
    public String toAddReader(Integer readerId, HttpServletRequest request){
        //判断readerId是否为空；
        if (null != readerId) {
            // 如果不为空，通过Id查询读者信息
            Reader reader = readerService.queryReaderById(readerId);
            // 将读者信息设置到请求域中
            request.setAttribute("reader",reader);
        }
        // 返回
        return "ww/addreader";
    }


}
